Page({
  data: {
    goods_list:[],
    // recive_person:"",
    // recive_phone:"",
    // recive_address:"",
    message:'',
    total_pay:0,
    total_num:0,
    express_price:0,
    is_default_address_hidden:false,
    is_no_address_hidden:false,
    address:[],
    default_address:{},

  },
  onLoad(query) {
    var app = getApp();
    if(app.globadData.isTourist == 'true'){
       app.getUserInfo();
     }
    // app.globadData.carts = [];
    // console.log( app.globadData.carts);
    // if(query.goods_id != undefined){
    //   //立即购买
    //   var app = getApp()
    //      var url = app.globadData.apiUrlHeader+'buy-now'
    //         // var id = this.data.goods_list.goods_id
    //            my.httpRequest({
    //                url: url, // 目标服务器 url
    //                method:'Post',
    //                headers:{'Content-Type':'Application/json'},
    //                data:JSON.stringify({
    //                 "goods_id":query.goods_id,
    //                  "access_token":app.globadData.access_token
    //                 }),
    //               dataType:'json',
    //               success: (res) => {
    //                  console.log(res.data.data[0]);
    //                  var to = parseFloat(res.data.data[0].good_selling_price);
                    
    //                  this.setData({
    //               goods_list : res.data.data,
    //           total_pay : to,
    //           total_num : 1
    //                  });
    //                },
    //            });

    // }
    // else //获取购物车产品、金额
     this.init();
     
     console.log(query.checkedAddressId);
     if(query.checkedAddressId != undefined){
       console.log('in if');
           var app = getApp();
            console.log(this.data);
            var url = app.globadData.apiUrlHeader+'get-address'
            // var id = this.data.goods_list.goods_id
               my.httpRequest({
                   url: url, // 目标服务器 url
                   method:'Post',
                   headers:{'Content-Type':'Application/json'},
                   data:JSON.stringify({
                    "address_id":query.checkedAddressId,
                     "access_token":app.globadData.access_token
                    }),
                  dataType:'json',
                  success: (res) => {
                     console.log(res.data);
                     this.setData({
                      //  address:res.data.data,
                       default_address : res.data.data,
                       is_default_address_hidden:false,
                            is_no_address_hidden:true,
                     });
                   },
               });
                          
                    
     }else{
       console.log('in else');
       
       this.getAddress();
     }
    },
    
    getAddress(){
      var app = getApp();
            console.log(this.data);
            var url = app.globadData.apiUrlHeader+'get-address'
            // var id = this.data.goods_list.goods_id
               my.httpRequest({
                   url: url, // 目标服务器 url
                   method:'Post',
                   headers:{'Content-Type':'Application/json'},
                   data:JSON.stringify({
                     "access_token":app.globadData.access_token
                    }),
                  dataType:'json',
                  success: (res) => {
                     console.log(res.data);
                     var def = {};
                     
                     for(var i=0;i<res.data.data.length;i++){
                       if(res.data.data[i].is_default ==1){
                           def = res.data.data[i];
                          }
                          
                      
                      //  res.data.data.quantity = parseInt(res.data.data.quantity);
                    }
                    
                    
                      if(res.data.status != -1){
                           this.setData({
                            is_default_address_hidden:false,
                            is_no_address_hidden:true,
                        })
                       }else{
                          console.log('in else');
                           this.setData({
                            is_default_address_hidden:true,
                            is_no_address_hidden:false,
                         })
                       }
                     

                     this.setData({
                       address:res.data.data,
                       default_address : def
                     });
                   },
               });
    },

   //获取购物车产品 
  init(){
      var app = getApp();
      // app.globadData.carts = this.data.goods_list;
    

      // this.data.goods_list = app.globadData.carts;
      var total=0;
      var total_n =0;
      this.data.goods_list = [];
      for(var i=0;i<app.globadData.carts.length;i++){
          // console.log(this.data.goods_list[i]);
          // console.log(this.data.goods_list[i].good_is_checked);
          // console.log(this.data.goods_list[i].good_name);
            if(!app.globadData.carts[i].good_is_checked){
              continue;
            }
              this.data.goods_list.push(app.globadData.carts[i]);
            
            // number(2);
              total = parseFloat(total)+parseFloat(app.globadData.carts[i].good_selling_price)*app.globadData.carts[i].quantity;
              total_n = parseFloat(total_n) + parseFloat(app.globadData.carts[i].quantity);
      }

            console.log(this.data);
            this.setData({
              total_pay : total,
              total_num : total_n
            });
  },
            
            addNewAddressTap(){
              my.redirectTo({
                 url: '/pages/main/address/address?from_page=settleAccount', // 需要跳转的应用内非 tabBar 的页面的路径，路径后可以带参数。参数与路径之间使用
                          });
                        },
                        
           editAddressTap(){
              my.redirectTo({
                 url: '/pages/main/address/address?from_page=settleAccount', // 需要跳转的应用内非 tabBar 的页面的路径，路径后可以带参数。参数与路径之间使用
                          });
            },

   async submit(e){
                 
             var app = getApp();
             
             app.globadData.order_remark = e.detail.value.message;
             app.globadData.goods_list = this.data.goods_list;
             app.globadData.order_address_id = this.data.default_address.id;
             await app.submit_order();
             console.log('submit ok');
              my.redirectTo({
                 url: '/pages/main/order-list/order-list', // 需要跳转的应用内非 tabBar 的页面的路径，路径后可以带参数。参数与路径之间使用
                          });

   },
});
